/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.share;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.entity.share.CircleShare;
import com.boxin.ims.modules.wechat.entity.share.ShareConfig;
import com.boxin.ims.modules.wechat.service.UserDefineService;
import com.boxin.ims.modules.wechat.dao.share.CircleShareDao;

/**
 * 分享转发Service
 * @author Jakemanse
 * @version 2013-10-01
 */
@Component
@Transactional(readOnly = true)
public class CircleShareService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CircleShareService.class);
	
	@Autowired
	private CircleShareDao circleShareDao;
	
	@Autowired
	private QRCodeService qrCodeService;
	
	@Autowired
	private ShareConfigService shareConfigService;
	
	@Autowired
	private UserDefineService userDefineService;
	
	public CircleShare get(Long id) {
		return circleShareDao.findOne(id);
	}
	
	public Page<CircleShare> find(Page<CircleShare> page, CircleShare circleShare) {
		DetachedCriteria dc = circleShareDao.createDetachedCriteria();
		dc.createAlias("member", "member");
		if (circleShare.getUser()!=null && circleShare.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", circleShare.getUser().getId()));
		}
		
		if (circleShare.getUser()!=null && !StringUtils.isEmpty(circleShare.getUser().getMobile())){
			dc.add(Restrictions.like("member.mobile", "%"+circleShare.getUser().getMobile()+"%" ));
		}
		
		if (circleShare.getMember()!=null && !StringUtils.isEmpty(circleShare.getMember().getName())){
			dc.add(Restrictions.like("member.name", "%"+circleShare.getMember().getName()+"%" ));
		}
		
		if (!StringUtils.isEmpty(circleShare.getAwardName())){
			dc.add(Restrictions.like("awardName", "%"+circleShare.getAwardName()+"%" ));
		}
		
		if(circleShare.getCreateTime() !=null){
			String timeField = "createTime";
			if (StringUtils.isNotEmpty(circleShare.getStatus()) && BaseEntity.STATUS_CLEAR.equals(circleShare.getStatus())){
				timeField = "updateTime";
			}
			dc.add(Restrictions.ge(timeField,circleShare.getCreateTime() ));
		}
		
		if(circleShare.getEndTime() !=null){
			String timeField = "createTime";
			if (StringUtils.isNotEmpty(circleShare.getStatus()) && BaseEntity.STATUS_CLEAR.equals(circleShare.getStatus())){
				timeField = "updateTime";
			}
			dc.add(Restrictions.le(timeField,circleShare.getEndTime()));
		}
		
		
		if (!StringUtils.isEmpty(circleShare.getStatus())){
			dc.add(Restrictions.eq("status",  circleShare.getStatus()  ));
		}
		if (circleShare.getWin() != null){
			dc.add(Restrictions.eq("win",  circleShare.getWin()  ));
		}
		
		
		if (StringUtils.isNotEmpty(circleShare.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+circleShare.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", CircleShare.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return circleShareDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(CircleShare circleShare) {
		
		int width = QRCodeUtils.WIDTH;
		int height =QRCodeUtils.HEIGHT;
//		UserDefine userDefine  = userDefineService.getByUserId(circleShare.getUser().getId());
//		if(userDefine!=null){
//			if(userDefine.getQrcodeHeight()!=null){
//				height = userDefine.getQrcodeHeight();
//			}
//			if(userDefine.getQrcodeWidth()!=null){
//				width = userDefine.getQrcodeWidth();
//			}
//		}
		
		//生成二维码
		QRCode qrcode = new QRCode();
		String content = QRCodeUtils.generateCircleShareSN(circleShare.getUser().getId());
		qrcode.setContent(content);
		String path = Constants.getWechatResourceSavePath();
		path += DateUtils.getTodayString()+File.separator+content;
		FileUtils.createDirectory(path);
		QRCodeUtils.generateQRCodePNG(content, path, width, height);
		qrcode.setFileName(content+".png");
		qrcode.setPngPath(path+".png");
		qrCodeService.save(qrcode);	//保存二维码
		
		circleShare.setQrCode(qrcode);
		circleShareDao.save(circleShare);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		circleShareDao.deleteById(id);
	}
	/**
	 * @author Jakemanse
	 * @time 2013-10-2  上午11:21:10
	 * @function <p>  </p>
	 * @param id
	 * @param status
	 */
	@Transactional(readOnly = false)
	public void updateStatusById(Long id,String status) {
		circleShareDao.updateStatusById(id,status);
	}
	
}
